package com.zxe.freshserver.controller.admin;
import com.zxe.commonserver.result.PageResult;
import com.zxe.commonserver.result.Result;
import com.zxe.freshserver.service.FreshService;
import com.zxe.freshserver.pojo.Fresh;
import com.zxe.freshserver.pojo.FreshDTO;
import com.zxe.freshserver.pojo.FreshPageQueryDTO;
import com.zxe.freshserver.pojo.FreshVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * 生鲜管理
 */
@RestController("adminFreshController")
@RequestMapping("/admin")
@Api(tags = "管理端生鲜相关接口")
@Slf4j
public class FreshController {

    @Autowired
    private FreshService freshService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 新增生鲜
     * @param freshDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增生鲜")
    public Result insert(@RequestBody FreshDTO freshDTO) {
        log.info("新增生鲜：{}", freshDTO);
        freshService.insert(freshDTO);
        String key = "fresh_" + freshDTO.getCategoryId();
        cleanCache(key);
        return Result.success();
    }

    /**
     * 生鲜分页查询
     * @param freshPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("生鲜分页查询")
    public Result<PageResult> page(FreshPageQueryDTO freshPageQueryDTO) {
        log.info("生鲜分页查询：{}", freshPageQueryDTO);
        PageResult pageResult = freshService.pageQuery(freshPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 生鲜批量删除
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("生鲜批量删除")
    public Result delete(@RequestParam List<Long> ids) {
        log.info("生鲜批量删除：{}", ids);
        freshService.deleteBatch(ids);
        cleanCache("fresh_*");
        return Result.success();
    }

    /**
     * 根据id查询生鲜
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询生鲜")
    public Result<FreshVO> findById(@PathVariable Long id) {
        log.info("根据id查询生鲜：{}", id);
        FreshVO freshVO = freshService.findById(id);
        return Result.success(freshVO);
    }

    /**
     * 修改生鲜
     * @param freshDTO
     * @return
     */
    @PutMapping
    @ApiOperation("修改生鲜")
    public Result update(@RequestBody FreshDTO freshDTO) {
        log.info("修改生鲜：{}", freshDTO);
        freshService.update(freshDTO);
        cleanCache("fresh_*");
        return Result.success();
    }

    /**
     * 起售、停售生鲜
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("起售、停售生鲜")
    public Result startOrStop(@PathVariable Integer status, Long id) {
        log.info("起售、停售生鲜：{}, {}", status, id);
        freshService.startOrStop(status, id);
        cleanCache("fresh_*");
        return Result.success();
    }

    /**
     * 根据分类id查询分类是否关联具体生鲜
     * @param id
     * @return
     */
    @GetMapping("/count/{id}")
    @ApiOperation("根据分类id查询生鲜数量")
    public Integer countByCategoryId(@PathVariable("id") Long id) {
        Integer count = freshService.countByCategoryId(id);
        return count;
    }

    /**
     * 清理缓存数据
     * @param pattern
     */
    private void cleanCache(String pattern) {
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }

}
